package com.che.software.testato.business;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.che.software.testato.business.exception.ScriptCreationManagerException;
import com.che.software.testato.business.exception.ScriptSearchManagerException;
import com.che.software.testato.business.exception.ScriptUpdateManagerException;
import com.che.software.testato.domain.dao.IScriptDAO;
import com.che.software.testato.domain.dao.jdbc.exception.ScriptCreationDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.ScriptSearchDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.ScriptUpdateDAOException;
import com.che.software.testato.domain.entity.Script;
import com.che.software.testato.domain.entity.creation.ScriptCreation;
import com.che.software.testato.domain.entity.search.ScriptSearch;

/**
 * Manager dedicated to the scripts.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("scriptManager")
public class ScriptManager {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ScriptManager.class);

	/**
	 * Members.
	 */
	@Autowired
	private IScriptDAO scriptDAO;

	/**
	 * Creates the scripts for a given hierarchy.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param hierarchyId the hierarchy id.
	 * @param scripts the scripts to create.
	 * @since July, 2011.
	 * @throws ScriptCreationManagerException if an error occurs during the
	 *         creation.
	 */
	public void createScriptsFromHierarchy(int hierarchyId, List<ScriptCreation> scripts) throws ScriptCreationManagerException {
		LOGGER.debug("createScriptsFromHierarchy(" + hierarchyId + "," + scripts.size() + " scripts).");
		try {
			scriptDAO.createScriptsFromHierarchy(hierarchyId, scripts);
		} catch (ScriptCreationDAOException e) {
			throw new ScriptCreationManagerException(e);
		}
	}

	/**
	 * Checks if some script have been elicited for a given hierarchy.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param hierarchyId the hierarchy id.
	 * @return true if the hierarchy has already been elicitate, else false.
	 * @since July, 2011.
	 * @throws ScriptSearchManagerException if an error occurs during the
	 *         search.
	 */
	public boolean isScriptExistingFromHierarchyId(int hierarchyId) throws ScriptSearchManagerException {
		LOGGER.debug("isScriptExistingFromHierarchyId(" + hierarchyId + ").");
		try {
			return scriptDAO.isScriptExistingFromHierarchyId(hierarchyId);
		} catch (ScriptSearchDAOException e) {
			throw new ScriptSearchManagerException(e);
		}
	}

	/**
	 * Script search from a bean of criterions.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param searchBean the criterions to use for the search.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws ScriptSearchManagerException if an error occurs during the
	 *         search.
	 */
	public List<Script> searchScript(ScriptSearch searchBean) throws ScriptSearchManagerException {
		LOGGER.debug("searchScript().");
		try {
			return scriptDAO.searchScript(searchBean);
		} catch (ScriptSearchDAOException e) {
			throw new ScriptSearchManagerException(e);
		}
	}

	/**
	 * Updates a script from his object.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param scriptToUpdate the script object to update in the database.
	 * @since August, 2011.
	 * @throws ScriptUpdateManagerException if an error occurs during the
	 *         update.
	 */
	public void updateScript(Script scriptToUpdate) throws ScriptUpdateManagerException {
		LOGGER.debug("updateScript(" + scriptToUpdate.getScriptId() + ").");
		try {
			scriptDAO.updateScript(scriptToUpdate);
		} catch (ScriptUpdateDAOException e) {
			throw new ScriptUpdateManagerException(e);
		}
	}

	/**
	 * Getter for the private field value scriptDAO.
	 * 
	 * @return the scriptDAO field value.
	 */
	public IScriptDAO getScriptDAO() {
		return scriptDAO;
	}

	/**
	 * Setting a value to the scriptDAO field.
	 * 
	 * @param scriptDAO the value to set.
	 */
	public void setScriptDAO(IScriptDAO scriptDAO) {
		this.scriptDAO = scriptDAO;
	}
}